package org.mydroidshare.android;

import org.mydroidshare.android.R;

import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.content.Context;
import android.widget.Button;
import android.widget.Toast;
	

public class MydroidShare extends Activity {
	/** Called when the activity is first created. */
	
	private Spinner s1;
	private LocationManager lm;
    private LocationListener locationListener;
    
    private TextView latText;
    private TextView lngText;
    private Boolean isTracking=false;
    
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		String[] providers=loadProviders();
	    s1 = (Spinner) findViewById(R.id.spinner1);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                this, android.R.layout.simple_spinner_item,providers);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        s1.setAdapter(adapter);
        
    	latText = (TextView) findViewById(R.id.latText);
		lngText = (TextView) findViewById(R.id.lngText);
	
	        
		final Button gpsButton = (Button) findViewById(R.id.gpsButton);
		gpsButton.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				//LoadCoords();
				isTracking=!isTracking;
				if(isTracking){
					gpsButton.setText("Stop");
					 lm.requestLocationUpdates(
					            s1.getSelectedItem().toString(), 
					            0, 
					            0, 
					            locationListener);
					
				}else{
					gpsButton.setText("Track");
					lm.removeUpdates(locationListener);
				}
			}
		});
	   locationListener = new MyLocationListener();
		      
		 //---use the LocationManager class to obtain GPS locations---
        lm = (LocationManager) 
            getSystemService(Context.LOCATION_SERVICE);    
        
       
     


	}
    
	
	
	private String[] loadProviders(){
		LocationManager myManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
		List<String> providers= myManager.getProviders(true);
		return (String[])providers.toArray(new String[0]);
		
	}
	
	private class MyLocationListener implements LocationListener 
    {
        public void onLocationChanged(Location loc) {
            if (loc != null) {                
                /*Toast.makeText(getBaseContext(), 
                    "Location changed : Lat: " + loc.getLatitude() + 
                    " Lng: " + loc.getLongitude(), 
                    Toast.LENGTH_SHORT).show();
                */
            	latText.setText(""+(int)loc.getLatitude() * 1E6);
        		lngText.setText(""+(int)loc.getLongitude() * 1E6);
            }
        }

        public void onProviderDisabled(String provider) {
            // TODO Auto-generated method stub
        	Toast.makeText(getBaseContext(), 
                    "Location Provider :  " + provider +" disabled", 
                    Toast.LENGTH_SHORT).show();
        }

        public void onProviderEnabled(String provider) {
            // TODO Auto-generated method stub
        }

        public void onStatusChanged(String provider, int status, 
            Bundle extras) {
        	String statusStr="";
        	if(status==LocationProvider.AVAILABLE){
        		statusStr="AVAILABLE";
        	}else if(status==LocationProvider.OUT_OF_SERVICE){
        		statusStr="OUT_OF_SERVICE";
        	}else if(status==LocationProvider.TEMPORARILY_UNAVAILABLE){
        		statusStr="TEMPORARILY_UNAVAILABLE";
        	}else{
        		statusStr="UNKNOWN";
           }
           	Toast.makeText(getBaseContext(), 
                    "Location Provider [" + provider +"] StatusChanged : "+statusStr, 
                    Toast.LENGTH_SHORT).show();
        }
    }    


}